/*
 * @Author: your name
 * @Date: 2021-08-17 19:11:12
 * @LastEditTime: 2022-04-09 20:19:05
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \blog_web\src\router\index.js
 */
import Vue from 'vue'
import Router from 'vue-router'
//  每次跳转路由的进度条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 导入获取本地存储的
import db from '@/util/localstorage'

Vue.use(Router)

const router = new Router({
  routes: [
    {
      path: '',
      redirect: {
        name: 'login'
      }
    },
    {
      path: '/home',
      name: 'home',
      redirect: {
        name: 'article'
      },
      component: () => import('@/views/home/home.vue'),
      children: [
        {
          path: 'article',
          name: 'article',
          component: () => import('@/views/blog/Blog.vue')
        },
        {
          path: 'article/:id',
          name: 'detail',
          component: () => import('@/views/blog/detail/Detail.vue')
        },
        {
          path: 'diary',
          name: 'diary',
          component: () => import('@/views/diary/Diary.vue')
        },
        {
          path: 'message',
          name: 'message',
          component: () => import('@/views/message/Message.vue')
        },
        {
          path: 'news',
          name: 'news',
          component: () => import('@/views/news/news.vue')
        },
        {
          path: 'video',
          name: 'video',
          component: () => import('@/views/video/video.vue')
        },
        {
          path: 'me',
          name: 'me',
          component: () => import('@/views/me/Me.vue')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/login.vue')
    }
  ]
})
router.beforeEach((to, from, next) => {
  NProgress.start()
  window.scrollTo(0, 0)
  // console.log('beforeEach', db.get('token'));
  // 在路由跳转之前，判断是否有token
  if (!db.get('token')) {
    if (to.path != '/login') {
      next('/login')
      NProgress.done()
    }
  } else {
    next()
    NProgress.done()
  }
  next()
})
router.afterEach(() => {
  window.scrollTo(0, 0)
  NProgress.done()
})
export default router
